"""
数据采集模块
功能：支持多种数据源接入（日常巡查记录、舆情采集、110报警信息）
输入：事件描述文本数据
输出：原始事件数据存储
"""

import json
from typing import Dict, List

class DataCollector:
    def __init__(self):
        self.data_sources = ['manual', 'file_upload', 'api']
        self.raw_data_storage = []
    
    def collect_from_manual(self, event_data: Dict) -> bool:
        """
        从手动输入采集数据
        :param event_data: 事件数据字典
        :return: 是否采集成功
        """
        try:
            # 验证数据格式
            required_fields = ['title', 'description', 'source', 'timestamp']
            for field in required_fields:
                if field not in event_data:
                    raise ValueError(f"缺少必要字段: {field}")
            
            # 添加到存储
            self.raw_data_storage.append(event_data)
            return True
        except Exception as e:
            print(f"手动输入数据采集失败: {e}")
            return False
    
    def collect_from_file(self, file_path: str) -> bool:
        """
        从文件采集数据
        :param file_path: 文件路径
        :return: 是否采集成功
        """
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
                
            # 验证数据格式
            if not isinstance(data, list):
                raise ValueError("文件数据格式错误，应为列表")
                
            for item in data:
                required_fields = ['title', 'description', 'source', 'timestamp']
                for field in required_fields:
                    if field not in item:
                        raise ValueError(f"数据项缺少必要字段: {field}")
                
            # 添加到存储
            self.raw_data_storage.extend(data)
            return True
        except Exception as e:
            print(f"文件数据采集失败: {e}")
            return False
    
    def collect_from_api(self, api_data: List[Dict]) -> bool:
        """
        从API采集数据
        :param api_data: API返回的数据列表
        :return: 是否采集成功
        """
        try:
            # 验证数据格式
            if not isinstance(api_data, list):
                raise ValueError("API数据格式错误，应为列表")
                
            for item in api_data:
                required_fields = ['title', 'description', 'source', 'timestamp']
                for field in required_fields:
                    if field not in item:
                        raise ValueError(f"数据项缺少必要字段: {field}")
                
            # 添加到存储
            self.raw_data_storage.extend(api_data)
            return True
        except Exception as e:
            print(f"API数据采集失败: {e}")
            return False
    
    def get_raw_data(self) -> List[Dict]:
        """
        获取原始事件数据
        :return: 原始事件数据列表
        """
        return self.raw_data_storage
    
    def clear_data(self):
        """
        清空原始数据存储
        """
        self.raw_data_storage.clear()

# 使用示例
def main():
    collector = DataCollector()
    
    # 手动输入示例
    manual_data = {
        "title": "交通事故",
        "description": "在中山路与解放路交叉口发生一起两车相撞事故",
        "source": "日常巡查",
        "timestamp": "2023-05-15 14:30:00"
    }
    collector.collect_from_manual(manual_data)
    
    # API数据示例
    api_data = [
        {
            "title": "道路施工",
            "description": "南京路从人民广场到淮海路段进行道路施工",
            "source": "110报警",
            "timestamp": "2023-05-15 15:20:00"
        },
        {
            "title": "设施故障",
            "description": "地铁2号线人民广场站电梯故障停运",
            "source": "舆情采集",
            "timestamp": "2023-05-15 16:10:00"
        }
    ]
    collector.collect_from_api(api_data)
    
    # 获取所有数据
    all_data = collector.get_raw_data()
    print("采集到的原始数据:")
    for item in all_data:
        print(f"  {item['title']}: {item['description']} ({item['source']} {item['timestamp']})")

if __name__ == "__main__":
    main()